#include "PreHeader.h"
#include "LayerLogo.h"
#include "LayerMain.h"
#include "GlobalData.h"
#include "HBScore.h"
#include "HBShare.h"
#include "DialogScore.h"
#include "DialogChellange.h"
#include "HBUmeng.h"
#include "HBUtilies.h"
#include "HBWebBrowser.h"

#if CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID
#include "HBOffer.h"
#include "main.h"
#endif // CC_PLATFORM_ANDROID

typedef enum
{
    kMenuPlayOne,
    kMenuPlayTwo,
    kMenuChallenge,
    kMenuLeaderboard,
    kMenuMore,
} MenuTag;

CCScene* LayerLogo::scene()
{
    CCScene *pScene = CCScene::create();
    LayerLogo *layer = LayerLogo::create();
    pScene->addChild(layer);
    
    return pScene;
}

bool LayerLogo::init()
{
    CCLayer::init();
    
    setKeypadEnabled(true);
    
    float w2 = 160;
    float h2 = 240;
    
    CCSprite* bg = createImage("fullbg.jpg", w2, h2, this);
    bg->setScale(1.3);
    bg->runAction(CCRepeatForever::create(CCRotateBy::create(200, -360)));
    
    CCSpriteFrameCache* cache = CCSpriteFrameCache::sharedSpriteFrameCache();
    cache->addSpriteFramesWithFile("sheet.plist");
    
    CCSprite* frame = createAnimation("logo_tap", 2, 78, 445 - kStartPos, 0.3, this);
    HBSetRatioScale(frame);
    frame = createAnimation("logo_burst", 2, 209, 432 - kStartPos, 0.3, this);
    HBSetRatioScale(frame);
    frame = createAnimation("logo_btn", 2, 190, 457 - kStartPos, 0.3, this);
    HBSetRatioScale(frame);
    frame = createAnimation("logo_frame", 2, w2, 252 - kStartPos, 0.3, this);
    HBSetRatioScale(frame);
    
    //下面的框
    frame = createImageWithFrameName("logo_bottom.png", w2, 78 - kStartPos, this);
    HBSetRatioScale(frame);

    // 勋章
    for (int i = 0; i < 10; i++)
    {
        if (gLevelDetail[i].isClear)
            createImageWithFrameName("icon_medal.png", 28+29*i, 76 - kStartPos, this);
        else
            createImageWithFrameName("icon_medal_d.png", 28+29*i, 76 - kStartPos, this);
    }

    CCMenu* menu = createMenu(this);
    CCMenuItem* item;
    
    item = createMenuItemWithCache("btn_long.png", "btn_long_d.png", w2, 366 - kStartPos, kMenuPlayOne, menu, this, menu_selector(LayerLogo::_menuCallback));
    addMenuLabelWithStroke(item, getLocalizedString("Play Single"), getLocalizedString("fontNormal"), 24, ccYELLOW, 1, ccBLACK);
    
    item = createMenuItemWithCache("btn_long.png", "btn_long_d.png", w2, 311 - kStartPos, kMenuChallenge, menu, this, menu_selector(LayerLogo::_menuCallback));
    addMenuLabelWithStroke(item, getLocalizedString("Chellange"), getLocalizedString("fontNormal"), 24, ccYELLOW, 1, ccBLACK);
    
    item = createMenuItemWithCache("btn_short.png", "btn_short_d.png", w2, 256 - kStartPos, kMenuPlayTwo, menu, this, menu_selector(LayerLogo::_menuCallback));
    addMenuLabelWithStroke(item, getLocalizedString("Play Double"), getLocalizedString("fontNormal"), 24, ccYELLOW, 1, ccBLACK);

    item = createMenuItemWithCache("btn_short.png", "btn_short_d.png", w2, 201 - kStartPos, kMenuLeaderboard, menu, this, menu_selector(LayerLogo::_menuCallback));
    addMenuLabelWithStroke(item, getLocalizedString("Leaderboard"), getLocalizedString("fontNormal"), 24, ccYELLOW, 1, ccBLACK);

#if HBCHANNEL != 2
    item = createMenuItemWithCache("btn_short.png", "btn_short_d.png", w2, 146 - kStartPos, kMenuMore, menu, this, menu_selector(LayerLogo::_menuCallback));
    addMenuLabelWithStroke(item, getLocalizedString("More Game"), getLocalizedString("fontNormal"), 24, ccYELLOW, 1, ccBLACK);
#endif
    
    return true;
}

void LayerLogo::_shareCallback()
{
//    CCLog("Callback Success");
}

void LayerLogo::_menuCallback(CCMenuItem* sender)
{
    int tag = sender->getTag();
    
    SimpleAudioEngine::sharedEngine()->playEffect("Menu.wav");

    switch (tag)
    {
        case kMenuPlayOne:
            HBUmeng::event("Button", "Single");
            gGameMode = kGameModeNormal;
            gFingerCount = kFingerOne;
            CCDirector::sharedDirector()->replaceScene(LayerMain::scene());
            break;
        case kMenuPlayTwo:
            HBUmeng::event("Button", "Double");
            gGameMode = kGameModeNormal;
            gFingerCount = kFingerTwo;
            CCDirector::sharedDirector()->replaceScene(LayerMain::scene());
            break;
        case kMenuChallenge:
            HBUmeng::event("Button", "Chellange");
            DialogChellange::create(this);
            break;
        case kMenuLeaderboard:
            HBUmeng::event("Button", "Leaderboard");
//            gGameMode = kGameModeTest;
//            DialogScore::create(11, this);
            HBScore::showBoard(gLeaderboardName[0]);
            break;
        case kMenuMore:
            HBUmeng::event("Button", "MoreGame");
            if (HBUmeng::getParamValue("isMoreEnable") == 1)
            {
                HBWebBrowser::gotoUrl("http://www.happybluefin.com/applist");
            }
            else
            {
#if CC_TARGET_PLATFORM == CC_PLATFORM_IOS

                HBWebBrowser::gotoUrl("itms-apps://itunes.com/apps/limin");
#else
                HBOffer::openOffer(HBOffer_AdsMogo);
#endif
            }
            break;
//        case kMenuTest:
//            HBShare::shared()->shareTo(HBShare_Sina, "测试测试", this, callfunc_selector(LayerLogo::_shareCallback));
//            break;
        default:
            break;
    }
}


void LayerLogo::keyBackClicked()
{
#if CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID
    exitApplication();
#endif // CC_PLATFORM_ANDROID

#if (CC_TARGET_PLATFORM == CC_PLATFORM_IOS)
    CCDirector::sharedDirector()->end();
    exit(0);
#endif // CC_PLATFORM_IOS
}

void LayerLogo::keyMenuClicked()
{

}